﻿using LJSheng.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LJSheng.Web.Controllers
{
    public class userController : UController
    {
        //个人中心
        public ActionResult grzx()
        {
            try
            {
                #region 参数解密,解析参数
                JObject linjiansheng = JsonConvert.DeserializeObject(Common.DESRSA.DESDeljsheng(Common.LCookie.GetCookie("linjiansheng"))) as JObject;
                Guid gid = Guid.Parse(linjiansheng["gid"].ToString());
                using (EFDB db = new EFDB())
                {
                    var b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.loginid = b.loginid;
                    ViewBag.account = b.account;
                    ViewBag.real_name = b.real_name;
                    ViewBag.grade = b.grade;
                    ViewBag.sfz = b.sfz;
                    ViewBag.nickname = b.nickname;
                    ViewBag.picture = LJShengHelper.User + b.picture;
                    ViewBag.sfzpicture = LJShengHelper.SFZ + b.sfzpicture;
                    ViewBag.ewm = LJShengHelper.EWM + b.ewm;
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.area = b.area;
                    ViewBag.qx = b.qx;
                    ViewBag.sex = b.sex;
                    ViewBag.khh = b.khh;
                    ViewBag.khm = b.khm;
                    ViewBag.kahao = b.kahao;
                    ViewBag.number = b.number;
                    string gc = "";
                    switch (b.grade)
                    {
                        case "初级会员":
                            gc = "primary";
                            break;
                        case "中级会员":
                            gc = "info";
                            break;
                        case "高级会员":
                            gc = "warning";
                            break;
                        default:
                            break;
                    }
                    ViewBag.gc = gc;
                    //我的消费
                    var o = db.LOrder.Where(l => l.usergid == gid && l.pay_status == 1).ToList();
                    if (o != null)
                    {
                        ViewBag.wdxf = o.Sum(l => l.rmb);
                    }
                    
                }
                #endregion
            }
            catch(Exception err)
            {
                Common.LCookie.DelALLCookie();
            }
            return View();
        }

        #region 我的订单
        public ActionResult wddd()
        {
            return View();
        }
        /// <summary>
        /// 获取商家订单列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult orderlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine());
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                Guid usergid = Common.LCookie.GetUser();
                var b = db.LOrder.Where(l => l.usergid == usergid).GroupJoin(db.ShopList,
                    x => x.buygid,
                    y => y.gid,
                    (x, y) => new
                    {
                        x.gid,
                        x.addtime,
                        x.order_no,
                        x.amount,
                        x.rmb,
                        x.user_cash,
                        x.pay_status,
                        x.pay_time,
                        x.buygid,
                        x.zhekou,
                        y.FirstOrDefault().show,
                        y.FirstOrDefault().city,
                        y.FirstOrDefault().area,
                        y.FirstOrDefault().address,
                        y.FirstOrDefault().lxfs,
                        y.FirstOrDefault().name,
                        pj = db.PL.Where(p=>p.ddgid == x.gid && p.usergid == x.usergid).FirstOrDefault()
                    }).AsQueryable();
                if (paramJson["lx"].ToString() == "0")
                {
                    b = b.Where(l => l.pay_status == 2 && l.show!=3);
                    DateTime st = DateTime.Now.AddMinutes(-30);
                    b = b.Where(l => l.addtime >= st);
                }
                else
                {
                    b = b.Where(l => l.pay_status == 1);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                     pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        #region 我的推荐
        public ActionResult wdtj()
        {
            Guid gid = Common.LCookie.GetUser();
            using (EFDB db = new EFDB())
            {
                var b = db.LUser.Where(l => l.gid == gid).Select(l => new
                {
                    l.number,
                    l.usergid
                }).FirstOrDefault();
                ViewBag.number = b.number;
                if (b.usergid != null && b.usergid!=Guid.Parse("00000000-0000-0000-0000-000000000000"))
                {
                    var u = db.LUser.Where(l => l.gid == b.usergid).Select(l => new
                    {
                        l.number,
                        l.account,
                        l.real_name
                    }).FirstOrDefault();
                    ViewBag.account = u.account;
                    ViewBag.real_name = u.real_name;
                    ViewBag.n = u.number;
                }
                else
                {
                    ViewBag.account = "顶级";
                    ViewBag.real_name = "用户";
                    ViewBag.n = 0;
                }
                //var u = db.LUser.Where(l => l.gid == gid).GroupJoin(db.LUser,
                //    x => x.usergid,
                //    y => y.gid,
                //    (x, y) => new
                //    {
                //        x.number,
                //        account = Nullable<Guid>.Equals(x.usergid, null) ? "顶级" : y.FirstOrDefault().account,
                //        real_name = Nullable<Guid>.Equals(x.usergid, null) ? "用户" : y.FirstOrDefault().real_name,
                //        n = Nullable<Guid>.Equals(x.usergid, null) ? 0 : y.FirstOrDefault().number
                //    }).FirstOrDefault();
                //if (u != null)
                //{
                //    ViewBag.account = u.account;
                //    ViewBag.real_name = u.real_name;
                //    ViewBag.number = u.number;
                //    ViewBag.n = u.n;
                //}
                //我的推荐
                //ViewBag.wdtj = Helper.fs(db.LUser.Select(l => new User { gid = l.gid, usergid = l.usergid }).ToList(), usergid, 3);
            }
            return View();
        }
        /// <summary>
        /// 推荐人列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult userlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                Guid usergid = Common.LCookie.GetUser();
                var b = db.LUser.Where(l => l.usergid == usergid).Select(l => new
                {
                    l.account,
                    l.real_name,
                    l.addtime,
                    l.gid,
                    l.number,
                    grade = l.lx == 3 ? db.ShopList.Where(s => s.usergid == l.gid).FirstOrDefault().grade : l.grade
                }).AsQueryable();
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                     pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        #region 我的账户
        public ActionResult wdzh()
        {
            return View();
        }
        /// <summary>
        /// 会员提现列表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        [HttpPost]
        public ActionResult txlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                Guid usergid = Common.LCookie.GetUser();
                var b = db.Pay.Where(l => l.usergid == usergid).Select(l => new { l.pay_status,l.rmb,l.addtime,l.remarks }).AsQueryable();
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    pages = Math.Ceiling((double)count / (double)pagesize),
                     pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        //下单支付
        public ActionResult pay()
        {
            return View();
        }
        //去支付
        public ActionResult gopay()
        {
            return View();
        }
    }
}